package com.lzx.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.lzx.api.CommonResult;
import com.lzx.entity.Permission;
import com.lzx.service.PermissionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Permission)表控制层
 *
 * @author makejava
 * @since 2021-03-03 17:56:02
 */
@RestController
@RequestMapping("permission")
public class PermissionController {
    /**
     * 服务对象
     */
    @Resource
    private PermissionService permissionService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Permission selectOne(Integer id) {
        return this.permissionService.queryById(id);
    }

    /**
     * 获取所有权限菜单
     * @return
     */
    @RequestMapping("/menus")
    public CommonResult<List<Tree<String>>> getAll(){
        List<Permission> permissionList = permissionService.list();

        List<TreeNode<Integer>> collect = permissionList.stream().map(permission -> {
            Map<String, Object> map = new HashMap<>();
            map.put("level", permission.getLevel());
            map.put("path", permission.getPath());
            TreeNode<Integer> treeNode = new TreeNode<Integer>().setId(permission.getId())
                    .setName(permission.getAuthname())
                    .setParentId(permission.getPid())
                    .setExtra(map);
            return treeNode;
        }).collect(Collectors.toList());

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setChildrenKey("children");
        // 最大递归深度
        treeNodeConfig.setDeep(9);

        // 构建node列表
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(collect, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getName());
                    tree.putExtra("level", treeNode.getExtra().getOrDefault("level", 2));
                    tree.putExtra("path", treeNode.getExtra().getOrDefault("path", null));
                });
        System.out.println(JSONUtil.toJsonStr(treeNodes));

        return CommonResult.success(treeNodes);
    }

}
